package com.mw.mwreaderdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.IOException;

public class Cpu extends Activity {

	private Spinner spCardSlot;

	private Button btnCpuReset,btnSendCmd,btnPowerOff,btnGetRandom,btnReadBankNo;
	private Button btnSSCardInfo,btnJKCardInfo,btnOther,btnPhysicalUid,btnQRCodeOprate;

	private EditText etCmd,etResult;

	private ImageView imageView1;

	private int cardSlot = 0;

	private String cmdStr = "";

	private long beforeClick = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cpu);

		spCardSlot = (Spinner) findViewById(R.id.spCardSlot);
		btnCpuReset = (Button) findViewById(R.id.btnCpuReset);
		btnSendCmd = (Button) findViewById(R.id.btnSendCmd);
		btnPowerOff = (Button) findViewById(R.id.btnPowerOff);
		btnReadBankNo = (Button)findViewById(R.id.btnBankNo);
		btnGetRandom = (Button) findViewById(R.id.btnGetRandom);
		btnSSCardInfo = (Button) findViewById(R.id.btnSSCardInfo);
		btnJKCardInfo = (Button) findViewById(R.id.btnJKCardInfo);
		btnOther = (Button) findViewById(R.id.btnOther);
		btnPhysicalUid = (Button)findViewById(R.id.btnPhysicalUid);
		btnQRCodeOprate = (Button)findViewById(R.id.btnQRCodeOprate);

		etCmd = (EditText) findViewById(R.id.etCmd);
		etResult = (EditText) findViewById(R.id.etResult);
		imageView1 = (ImageView) findViewById(R.id.imageView2);
		// 将输入字符转换成大写
		etCmd.setTransformationMethod(new InputLowerToUpper());

		spCardSlot.setSelection(1);
		spCardSlot.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				cardSlot = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});

		btnCpuReset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}

					etResultAddStr(MainActivity.reader.smartCardReset(cardSlot, 0));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnSendCmd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.smartCardCommand(cardSlot, etCmd.getText().toString()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnPowerOff.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					MainActivity.reader.smartCardPowerDown(cardSlot);
					etResultAddStr("卡片已下电");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnGetRandom.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.smartCardCommand(cardSlot, "0084000008"));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		//读银行卡卡号
		btnReadBankNo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.ReadBankCard());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		//读社保卡基本信息
		btnSSCardInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.ReadSSCardInfo());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		//读居民健康卡基本信息
		btnJKCardInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.ReadJKCardInfo());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		//读二代证信息
		btnOther.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}

					//二代证状态


					//二代证SAM号
					etResultAddStr(MainActivity.readerAndroidUsb.ReadIDCardSamA());

					//二代证文字信息
					etResultAddStr(MainActivity.readerAndroidUsb.ReadIDCardInfo());

					//二代证图片信息
					Log.i("mwcard", "图片信息："+MainActivity.readerAndroidUsb.reveDataP);
					if(MainActivity.readerAndroidUsb.reveDataP == "1")
					{
						try {
							FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory() + "/wltlib/zp.bmp");
							Bitmap bmp = BitmapFactory.decodeStream(fis);
							fis.close();

							imageView1.setVisibility(View.VISIBLE);
							imageView1.setImageBitmap(bmp);
						} catch (IOException e) {
							// TODO 自动生成的 catch块
							e.printStackTrace();
						}
					}
					else
					{
						//ett.append("照片解码失败，请检查路径" + Environment.getExternalStorageDirectory() + "/wltlib/");
						//imageview1.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.face));
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		//二维码引擎时长
		btnQRCodeOprate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.QRCodeOprate(15));
					//etResultAddStr(""+MainActivity.reader.QRCodeSetVoctips(0));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		//读二代证物理卡号
		btnPhysicalUid.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(Cpu.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.IDCardPhysicalUid());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		// 设置双击操作信息提示框清除数据
		etResult.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (System.currentTimeMillis() - beforeClick > 500) {
						beforeClick = System.currentTimeMillis();
					} else {
						etResult.setText("");
					}
				}
				return false;
			}
		});
	}

	/*
	 * @Description 提示信息框添加信息
	 *
	 * @param str 要添加的信息
	 *
	 * @return void
	 */
	private void etResultAddStr(String str) {
		etResult.setText(etResult.getText() + " " + str + "\n");
		etResult.setSelection(etResult.length());// 调整光标到最后一行
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cpu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
